<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .box1 {
        margin: 20px;
        width: 100px;
        height: 100px;
        /* background-color: red; */
    }

    .box2 {
        width: 100px;
        height: 100px;
        /* background-color: rgb(38, 255, 0); */
    }
</style>

<body>
    <div class="box1"></div>
    <div class="box2"></div>
</body>

</html>
<!-- <script> -->
    <!-- //     function color(){
    // //16进制随机数生成 颜色值
    // var r = Math.floor(Math.random() * 256);
    // var g = Math.floor(Math.random() * 256);
    // var b = Math.floor(Math.random() * 256);
    // var color = '#' + r.toString(16) + g.toString(16) + b.toString(16);
    // return color;
    // }

//     function randomColor() {
//         return "rgba(" + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + "," + parseInt(Math
//             .random() * 255) + ",1)";
//     }
//   randomColor() -->
<!-- </script> -->  


<script>
    function getRandomColor(flag) {
        let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f']
        if (flag) {
            let str = '#';
            for (let i = 0; i < 6; i++) {
                let arrA = Math.floor(Math.random() * 16)
                str += arr[arrA]
            }
            return str
        } else {
            let r = Math.floor(Math.random() * 256);
            let g = Math.floor(Math.random() * 256);
            let b = Math.floor(Math.random() * 256);


            return `rgb(${r},${g},${b})`;
        }
    };


    console.log(getRandomColor(true)); //#fabc12
    console.log(getRandomColor(false)); //rgb(128,65,88)
</script>



 